package com.bjesc_b2c.app.appraiset.widget.popupwindow;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.CarSeriesModelAdapter;
import com.bjesc_b2c.app.appraiset.adapter.CarfromOneAdapter;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.widget.BasePopupWindow;
import com.bjesc_b2c.app.appraiset.widget.view.IndexView;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import master_bean.CarForBean;
import master_bean.CarSeriesModelBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

public class CarStatusPop extends BasePopupWindow implements MultiItemTypeAdapter.OnItemClickListener, IndexView.OnSetIndexListenre {
    private RecyclerView recyclerView;
    private RecyclerView recyclerView_series;
    private IndexView indexView;
    private CarfromOneAdapter oneAdapter;
    private List<CarForBean> list = new ArrayList<CarForBean>();
    private LinearLayoutManager manager;
    private Context context;
    private CarBrandSelectListener selectListener;
    private int type;
    private CarSeriesModelAdapter carCeriesAdapter;

    public CarStatusPop(Context context, List<CarForBean> brandList, CarBrandSelectListener selectListener, int type) {
        super(context, null);
        this.context = context;
        list.clear();
        list.addAll(brandList);
        this.selectListener = selectListener;
        this.type = type;
        Logger.d(brandList.get(0).getName());
        oneAdapter = new CarfromOneAdapter(context, R.layout.layout_item_carfrom_one, list, type);
        indexView.setVisibility(View.GONE);
        manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(oneAdapter);
        recyclerView.setOnScrollListener(scrollListener);
        oneAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initViews() {
        view = inflater.inflate(R.layout.layout_pop_car_brand, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView_series = (RecyclerView) view.findViewById(R.id.recycler_view_series);
        indexView = (IndexView) view.findViewById(R.id.index_view);
        indexView.setOnSetIndexListenre(this);
    }

    @Override
    protected void setView() {
        super.setView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected int setRootViewId() {
        return R.id.root;
    }


    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int position = manager.findFirstVisibleItemPosition();
            indexView.refresh(list.get(position + 1).getFirstLetter().charAt(0));
        }
    };


    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        if (type == CarfromOneAdapter.TYPE_ONE) {
            getCarSeries(list.get(position).getId());
            recyclerView_series.setVisibility(View.GONE);
        }
        for (int i = 0; i < list.size(); i++) {
            if (i == position) {
                list.get(i).setCheck(true);
            } else {
                list.get(i).setCheck(false);
            }
        }
        if (type == CarfromOneAdapter.TYPE_TWO) {
            selectListener.getBrandId(list.get(position).getId(), list.get(position).getName());
            oneAdapter.notifyDataSetChanged();
            dismiss();
        }
    }

    private void getCarSeries(String brandId) {
        HashMap<String, Object> build = new MapUtil()
                .put("brand_id", brandId)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getCarSeries()
                , new BaseSubscriber<TokenBean<List<CarSeriesModelBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<CarSeriesModelBean>> o) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            initViewSeries(o.getData());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    private void initViewSeries(final List<CarSeriesModelBean> data) {
        recyclerView_series.setVisibility(View.VISIBLE);
        carCeriesAdapter = new CarSeriesModelAdapter(context, R.layout.layout_item_select_car_brand, data);
        recyclerView_series.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView_series.setAdapter(carCeriesAdapter);
        carCeriesAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                selectListener.getBrandId(data.get(position).getId(), data.get(position).getName());
                dismiss();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    public interface CarBrandSelectListener {
        void getBrandId(String brandId, String brandName);
    }

    @Override
    public void setIndexListenre(char index) {
        for (int i = 0; i < list.size(); i++) {
            if (String.valueOf(index).equals(list.get(i).getFirstLetter())) {
                manager.scrollToPositionWithOffset(i, 0);
                break;//加了break显示才会更准确—yd
            }
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}